import { useEffect } from 'react';
import Modals from '@/components/modals/Modals';
import { Form, Input, message, ModalProps } from 'antd';
import { GetListType } from '@/common/types';

interface Props extends ModalProps {
  defaultValue?: GetListType;
  open: boolean;
  onCancel: () => void;
  onSubmit: (values: any) => void;
}

const EditModal = ({ defaultValue, onSubmit, ...props }: Props) => {
  const [messageApi, contextHolder] = message.useMessage();
  const [form] = Form.useForm();
  useEffect(() => {
    form.setFieldsValue(defaultValue);
  }, [defaultValue]);

  const onFinish = (values: GetListType) => {
    if (values.dataset_tag.trim() === '') {
      messageApi.warning('不允许空文件类型上传!');
      return;
    }
    onSubmit(values);
  };

  const handleOk = () => {
    form.submit();
  };

  return (
    <>
      {contextHolder}
      <Modals {...props} title="编辑文档类型" onOk={handleOk}>
        <Form layout="vertical" form={form} onFinish={onFinish}>
          <Form.Item label="文档类型" name={'dataset_tag'} style={{ marginBottom: 0 }}>
            <Input className="!border-0 !bg-input-gray !text-base" showCount maxLength={20} />
          </Form.Item>
        </Form>
      </Modals>
    </>
  );
};

export default EditModal;
